package com.stdspringboot.service.impl;

import com.stdspringboot.entity.User;
import com.stdspringboot.mapper.IUserMapper;
import com.stdspringboot.service.ILoginService;
import com.stdspringboot.vo.JsonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

//声明该类是 service 层，并且有 springboot 统一管理该类对象的创建、销毁
@Service("LoginSrvImpl")
public class LoginSrvImpl implements ILoginService {

    @Resource
    private IUserMapper mapper;

    public JsonResult login(String username, String password) throws Exception {

        User user=new User();
        user.setUsername(username);
        user.setPassword(password);

        List list=mapper.login(user);

        JsonResult jr=null;
        if(list==null || list.size()==0){//根据用户名、密码没查询出来
            jr=new JsonResult(JsonResult.CODE_LOGIN_FAIL,"登录失败，请重新输入用户名或密码！");
        }else{
            User userDB=(User)list.get(0);
            userDB.setPassword("");//不显示密码
            jr=new JsonResult(JsonResult.CODE_SUCCESS,"登录成功！");
            jr.setData(userDB);
        }

        return jr;
    }
}
